package com.buka.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buka.constans.SystemConstants;
import com.buka.domain.ResponseResult;
import com.buka.mapper.MenuMapper;
import com.buka.pojo.Menu;
import com.buka.pojo.RoleMenu;
import com.buka.service.MenuService;
import com.buka.service.RoleMenuService;
import com.buka.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【sys_menu(菜单权限表)】的数据库操作Service实现
 * @createDate 2024-05-28 16:35:11
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public List<String> selectPermsByUserId(Long id) {
        //如果是管理员，返回所有的权限
        if (id == 1L) {
            LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(Menu::getMenuType, SystemConstants.MENU, SystemConstants.BUTTON);
            wrapper.eq(Menu::getStatus, SystemConstants.STATUS_NORMAL);
            List<Menu> menus = list(wrapper);
            List<String> perms = menus.stream().map(Menu::getPerms).collect(Collectors.toList());
            return perms;
        }
        //否则返回所具有的权限
        return getBaseMapper().selectPermsByUserId(id);
    }

    @Override
    public List<Menu> selectRouterMenuTreeByUserId(Long userId) {
        MenuMapper menuMapper = getBaseMapper();
        List<Menu> menus = null;
        if (userId == 1l) {
            //超级管理员
            menus = menuMapper.selectAllRouterMenu();
        } else {
            //不是管理员
            menus = menuMapper.selectRouterMenuTreeByUserId(userId);
        }
        List<Menu> menuList = builderMenuTree(menus, 0L);
        return menuList;
    }

    @Override
    public ResponseResult list(int status, String menuName) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        if (status > -1) {
            queryWrapper.eq(Menu::getStatus, status);
        }
        if (StringUtils.hasText(menuName)) {
            queryWrapper.like(Menu::getMenuName, menuName);
        }
        List<Menu> menuList = list(queryWrapper);
        return ResponseResult.okResult(menuList);
    }

    @Override
    public ResponseResult add(Menu menu) {
        save(menu);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getMenuById(int id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult treeSelect() {
        List<Menu> list = list();
        List<MenuVo> menuVoList = list.stream().map(menu -> new MenuVo(menu.getId().intValue(), menu.getMenuName(), menu.getParentId().intValue(), null)).collect(Collectors.toList());
        List<MenuVo> menuVos = builderMenuVoTree(menuVoList, 0);
        return ResponseResult.okResult(menuVos);
    }

    @Override
    public ResponseResult roleMenuTreeselect(int id) {
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRoleId, id);
        List<RoleMenu> roleMenuList = roleMenuService.list(queryWrapper);
        List<Integer> menuIds = roleMenuList.stream().map(roleMenu -> roleMenu.getMenuId().intValue()).collect(Collectors.toList());
        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        menuLambdaQueryWrapper.in(Menu::getId, menuIds);
        List<Menu> menuList = list(menuLambdaQueryWrapper);
        List<MenuVo> menuVoList = menuList.stream().map(menu -> new MenuVo(menu.getId().intValue(), menu.getMenuName(), menu.getParentId().intValue(), null)).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("menus", menuVoList);
        map.put("checkedKeys", menuIds);
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult deleteMenu(int id) {
        getBaseMapper().deleteById(id);
        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        menuLambdaQueryWrapper.eq(Menu::getParentId, id);
        getBaseMapper().delete(menuLambdaQueryWrapper);
        return ResponseResult.okResult();
    }

    private List<MenuVo> builderMenuVoTree(List<MenuVo> menuVos, int i) {
        //1,先查询出来所有的根菜单
        List<MenuVo> l1 = new ArrayList<>();
        for (MenuVo menu : menuVos) {
            if (menu.getParentId() == i) {
                //查询跟菜单下的子菜单
                List<MenuVo> l2 = builderMenuVoTree(menuVos, menu.getId());
                menu.setChildren(l2);
                l1.add(menu);
            }
        }
        return l1;
    }

    private List<Menu> builderMenuTree(List<Menu> menus, Long i) {
        //1,先查询出来所有的根菜单
        List<Menu> l1 = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId().equals(i)) {
                //查询跟菜单下的子菜单
                List<Menu> l2 = builderMenuTree(menus, menu.getId());
                menu.setChildren(l2);
                l1.add(menu);
            }
        }
        return l1;
    }


}




